#include "InBlock.h"
#include "Keywords.h"
#include "StringOps.h"

InBlock::InBlock(Scope* pParentScope)
{
    m_eType=Scope::eScopeBlock;
    m_pxParent = pParentScope;
	m_sScopeEnder = "}";

	classify();
}

InBlock::~InBlock(void)
{

}

void InBlock::classify()
{
	m_eBlockType = eBlockGeneric;

	std::string sTemp;
	std::string sTempName;
	std::string sTempType;

	std::string sTestKeyword=m_pxParent->m_sLastKeyword;
	sTemp = m_pxParent->m_asElement.back();

	splitAsNameAndType(sTemp, sTempName, sTempType);
	//TODO Split at last instance of :
	//TODO Split second half with ,
	//TODO Last word of each ,-split piece is symbol

	if(Keywords::isBlockKeyword(sTestKeyword))
	{
		m_eBlockType = eBlockKeyword;
		m_sName = sTestKeyword;

	}

	if(Keywords::isDataTypeDefiner(sTestKeyword))
	{
		if(sTestKeyword=="namespace")
			m_eBlockType = eBlockNamespace;
		else
			m_eBlockType= eBlockClass;

		m_sName = sTempName;
	}

	if(m_pxParent->m_apxChild.size()>0)
	{
		Scope* pxScopeSibling = m_pxParent->m_apxChild.back();
		if(pxScopeSibling->getScopeType() == eScopeParen)
		{
			if(m_pxParent->canDefineFunction())
				if(m_eBlockType!= eBlockClass) //Fix for Type after function declarations
				{
					m_eBlockType = eBlockFunction;
					m_sName = sTempName;
				}
		}
	}

	return;
}

bool InBlock::canDefineVariable()
{
	if(!Scope::canDefineVariable())
		return false;

	if(m_eBlockType == eBlockClass)
		return false;

	return true;
}

bool InBlock::canDefineType()
{
	return true;
}

bool InBlock::canDefineFunction()
{
	switch(m_eBlockType)
	{
	case eBlockNamespace:
	case eBlockClass:
		return true;
	}
	return false;
}

bool InBlock::isFunction()
{
	switch(m_eBlockType)
	{
	case eBlockFunction:
		return true;
	}
	return false;
}

Scope* InBlock::parse(Token& inToken, bool &bIsEndOfScope)
{
    Scope* pxScope = NULL;
    bIsEndOfScope=false;

    pxScope = Scope::parse(inToken, bIsEndOfScope);

	if(inToken.m_sValue == m_sScopeEnder)
    {
		bIsEndOfScope= true;
    }

    return pxScope;
}